package com.lemon.ui.codemaker;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.plaf.FontUIResource;

import org.apache.commons.lang3.StringUtils;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.lemon.components.ant.MavenService;
import com.lemon.components.ant.beans.ProjectInfo;
import com.lemon.components.ant.docmaker.action.ExportMysqlAction;
import com.lemon.components.ant.project.ProjectMaker;
import com.lemon.components.ant.project.beans.ServiceInfo;
import com.lemon.components.ant.project.makers.Controller4RestMaker;
import com.lemon.components.ant.project.makers.ControllerMaker;
import com.lemon.components.ant.project.makers.ServiceDTOMaker;
import com.lemon.components.ant.project.makers.ServiceMaker;
import com.lemon.components.ant.util.ConfigUtil;
import com.lemon.components.ant.util.PathUtil;
import com.lemon.components.docs.mysql.MysqlLocalTool;
import com.lemon.ui.codemaker.JacksonParser.JacksonParserUI;
import com.lemon.ui.codemaker.component.RecentlyFile;
import com.lemon.ui.codemaker.namecover.NameCoverWindow;
import com.lemon.ui.codemaker.window.MybatisGenWindow;
import com.lemon.ui.codemaker.window.ProcessWindow;

public class CodeMaker {
	
	private static void initGlobalFont(Font font) {
		FontUIResource fontRes = new FontUIResource(font);
		for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource) {
				UIManager.put(key, fontRes);
			}
		}
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					CodeMaker window = new CodeMaker();
					window.frmApplication.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	private JFrame frmApplication;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JPasswordField passwordField;
	private JTextField textField_6;
	private JTextField textField_10;
	private JTextField textField_11;
	private JTextField textField_12;
	private JCheckBox chckbxNewCheckBox;
	private JMenu recentlyMenu;
	private JSpinner spinner;
	private Properties config = new Properties();

	private JTextField textField_4;

	private JTextField textField_5;

	/**
	 * Create the application.
	 */
	public CodeMaker() {
		initialize();
	}
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		initGlobalFont(new Font("宋体", Font.PLAIN, 12));
		frmApplication = new JFrame();
		frmApplication.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/code/icon/logo.png")));
		frmApplication.setResizable(false);
		frmApplication.setBounds(100, 100, 700, 557);
		frmApplication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmApplication.getContentPane().setLayout(new BorderLayout(0, 0));
		frmApplication.setLocationRelativeTo(null);
		
		JPanel buttonPanel = new JPanel();
		frmApplication.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JProgressBar progressBar = new JProgressBar();
		progressBar.setStringPainted(true);
		buttonPanel.add(progressBar);
		
		JSeparator separator = new JSeparator();
		separator.setPreferredSize(new Dimension(265, 0));
		buttonPanel.add(separator);
		
		JButton btnNewButton_2 = new JButton("连接测试");
		btnNewButton_2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ProjectInfo projectInfo = new ProjectInfo();
				projectInfo.setDbhost(textField_8.getText());
				projectInfo.setDbname(textField_7.getText());
				projectInfo.setDbusername(textField_9.getText());
				projectInfo.setDbpasswd(new String(passwordField.getPassword()));
				try {
					MysqlLocalTool.getConWithException(projectInfo);
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(frmApplication, e2.getMessage(), "测试连接", JOptionPane.ERROR_MESSAGE);
					return;
				}
				JOptionPane.showMessageDialog(frmApplication, "数据库连接成功", "测试连接", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		buttonPanel.add(btnNewButton_2);
		
		JButton btnNewButton_1 = new JButton("刷新配置");
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				initializeForm();
			}
		});
		buttonPanel.add(btnNewButton_1);
		
		JButton btnNewButton = new JButton("保存配置");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				saveConfig();
				progressBar.setValue(0);
				ConfigUtil.getInstance(RecentlyFile.configFilePath).reloadProperties();
				JOptionPane.showMessageDialog(frmApplication, "配置更新成功", "保存配置", JOptionPane.INFORMATION_MESSAGE);
				progressBar.setValue(100);
			}
		});
		
		buttonPanel.add(btnNewButton);
		
		JPanel formPanel = new JPanel();
		frmApplication.getContentPane().add(formPanel, BorderLayout.CENTER);
		formPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		JPanel basicPanel = new JPanel();
		basicPanel.setPreferredSize(new Dimension(10, 11));
		basicPanel.setBorder(new TitledBorder(null, "\u57FA\u7840\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		formPanel.add(basicPanel, "2, 2, fill, fill");
		basicPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel = new JLabel("工作空间目录");
		basicPanel.add(lblNewLabel, "2, 2, right, default");
		
		textField = new JTextField();
		textField.setEditable(false);
		basicPanel.add(textField, "4, 2, fill, default");
		textField.setColumns(10);
		
		JButton btnNewButton_3 = new JButton("浏览...");
		btnNewButton_3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser jfileChooser = new JFileChooser(new File(textField_1.getText()));
				jfileChooser.setFileSelectionMode(1);
				if(jfileChooser.showOpenDialog(frmApplication) == JFileChooser.APPROVE_OPTION) {
					textField.setText(jfileChooser.getSelectedFile().getAbsolutePath().replaceAll("\\\\", "/"));
				}
			}
		});
		basicPanel.add(btnNewButton_3, "6, 2");
		
		JLabel lblNewLabel_1 = new JLabel("项目编码");
		basicPanel.add(lblNewLabel_1, "10, 2, right, default");
		
		textField_1 = new JTextField();
		basicPanel.add(textField_1, "12, 2, fill, default");
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("公司英文名");
		basicPanel.add(lblNewLabel_2, "2, 4, right, default");
		
		textField_2 = new JTextField();
		basicPanel.add(textField_2, "4, 4, fill, default");
		textField_2.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("项目描述");
		basicPanel.add(lblNewLabel_3, "10, 4, right, default");
		
		textField_3 = new JTextField();
		basicPanel.add(textField_3, "12, 4, fill, default");
		textField_3.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("服务端口");
		basicPanel.add(lblNewLabel_6, "2, 6, right, default");
		
		spinner = new JSpinner();
		spinner.setEditor(new JSpinner.NumberEditor(spinner, "0"));
		basicPanel.add(spinner, "4, 6");
		
		JPanel mysqlPanel = new JPanel();
		mysqlPanel.setPreferredSize(new Dimension(10, 11));
		mysqlPanel.setBorder(new TitledBorder(null, "\u6570\u636E\u5E93\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		formPanel.add(mysqlPanel, "2, 4, fill, fill");
		mysqlPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel_4 = new JLabel("连接地址");
		mysqlPanel.add(lblNewLabel_4, "2, 2, right, default");
		
		textField_8 = new JTextField();
		mysqlPanel.add(textField_8, "4, 2, fill, default");
		textField_8.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("数据库名称");
		mysqlPanel.add(lblNewLabel_5, "8, 2, right, default");
		
		textField_7 = new JTextField();
		mysqlPanel.add(textField_7, "10, 2, fill, default");
		textField_7.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel("用户名");
		mysqlPanel.add(lblNewLabel_8, "2, 4, right, default");
		
		textField_9 = new JTextField();
		mysqlPanel.add(textField_9, "4, 4, fill, default");
		textField_9.setColumns(10);
		
		JLabel lblNewLabel_9 = new JLabel("密码");
		mysqlPanel.add(lblNewLabel_9, "8, 4, right, default");
		
		passwordField = new JPasswordField();
		mysqlPanel.add(passwordField, "10, 4, fill, default");
		
		JLabel lblNewLabel_7 = new JLabel("初始化DB");
		mysqlPanel.add(lblNewLabel_7, "2, 6");
		
		chckbxNewCheckBox = new JCheckBox("执行初始化");
		mysqlPanel.add(chckbxNewCheckBox, "4, 6");
		
		
		JPanel servicePanel = new JPanel();
		servicePanel.setPreferredSize(new Dimension(10, 11));
		servicePanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u670D\u52A1\u4FE1\u606F ", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		formPanel.add(servicePanel, "2, 6, fill, fill");
		servicePanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		JLabel lblNewLabel_10 = new JLabel("服务名称");
		servicePanel.add(lblNewLabel_10, "2, 2, right, default");
		
		textField_10 = new JTextField();
		servicePanel.add(textField_10, "4, 2, fill, default");
		textField_10.setColumns(10);
		
		JLabel lblNewLabel_11 = new JLabel("Model代码");
		servicePanel.add(lblNewLabel_11, "8, 2, right, default");
		
		textField_6 = new JTextField();
		textField_6.setEditable(false);
		servicePanel.add(textField_6, "10, 2, fill, default");
		textField_6.setColumns(10);
		
		JButton btnNewButton_6 = new JButton("选择...");
		btnNewButton_6.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModelSelect modelSelect = new ModelSelect(textField.getText(),textField_1.getText(),textField_2.getText());
				modelSelect.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				modelSelect.setModal(true);
				modelSelect.setLocationRelativeTo(frmApplication);
				modelSelect.setVisible(true);
				if(StringUtils.isNotBlank(modelSelect.getSelectedValue())) {
					textField_6.setText(StringUtils.uncapitalize(modelSelect.getSelectedValue()));
					textField_10.setText(StringUtils.uncapitalize(modelSelect.getSelectedValue()));
				}
			}
		});
		servicePanel.add(btnNewButton_6, "12, 2");
		
		JPanel panel = new JPanel();
		servicePanel.add(panel, "4, 6, 9, 1, right, fill");
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JButton btnNewButton_7 = new JButton("重新生成参数");
		btnNewButton_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				progressBar.setValue(50);
				ServiceDTOMaker serviceDTOMaker = new ServiceDTOMaker();
				ServiceInfo serviceInfo = ConfigUtil.getInstance(RecentlyFile.configFilePath).getServiceInfo();
				serviceDTOMaker.makeServiceDTO(serviceInfo);
				progressBar.setValue(100);
			}
		});
		
		JButton btnNewButton_8 = new JButton("生成Boss控制器");
		btnNewButton_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				progressBar.setValue(50);
				Controller4RestMaker.make(ConfigUtil.getInstance(RecentlyFile.configFilePath).getControllerInfo());
				progressBar.setValue(100);
			}
		});
		panel.add(btnNewButton_8);
		panel.add(btnNewButton_7);
		
		JMenuBar menuBar = new JMenuBar();
		frmApplication.setJMenuBar(menuBar);
		
		JMenu mnNewMenu_2 = new JMenu("文件");
		menuBar.add(mnNewMenu_2);
		
		JMenuItem mntmNewMenuItem_5 = new JMenuItem("打开...");
		mntmNewMenuItem_5.setIcon(new ImageIcon(this.getClass().getResource("/code/icon/open.png")));
		
		mnNewMenu_2.add(mntmNewMenuItem_5);
		
		JMenuItem mntmNewMenuItem_7 = new JMenuItem("保存");
		mntmNewMenuItem_7.setIcon(new ImageIcon(this.getClass().getResource("/code/icon/save.png")));
		mntmNewMenuItem_7.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				saveConfig();
				progressBar.setValue(0);
				ConfigUtil.getInstance(RecentlyFile.configFilePath).reloadProperties();
				JOptionPane.showMessageDialog(frmApplication, "配置更新成功", "保存配置", JOptionPane.INFORMATION_MESSAGE);
				progressBar.setValue(100);
			}
		});
		
		recentlyMenu = new JMenu("打开最近的配置");
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfileChooser = new JFileChooser();
				jfileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				jfileChooser.setFileFilter(new FileNameExtensionFilter("codemaker配置文件","cmk"));
				if(jfileChooser.showOpenDialog(frmApplication) == JFileChooser.APPROVE_OPTION) {
					try {
						ConfigUtil.getInstance(RecentlyFile.configFilePath).loadPropertiesFrom(jfileChooser.getSelectedFile().getAbsolutePath());
						RecentlyFile.addFilePath(jfileChooser.getSelectedFile().getAbsolutePath(),recentlyMenu);
					} catch (FileNotFoundException e1) {
						JOptionPane.showMessageDialog(frmApplication, "cmk配置文件未找到", "加载配置失败", JOptionPane.ERROR_MESSAGE);
					} catch (IOException e1) {
						JOptionPane.showMessageDialog(frmApplication, "打开配置失败，原因是："+e1.getMessage(), "加载配置失败", JOptionPane.ERROR_MESSAGE);
					}
					initializeForm();
					progressBar.setValue(100);
					frmApplication.setTitle("CodeMaker - "+ jfileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		mnNewMenu_2.add(recentlyMenu);
		
		JMenuItem mntmNewMenuItem_13 = new JMenuItem("导出数据库文档...");
		var exportMysqlAction = new ExportMysqlAction();
		exportMysqlAction.setParent(frmApplication);
		mntmNewMenuItem_13.addActionListener(exportMysqlAction);
		mnNewMenu_2.add(mntmNewMenuItem_13);
		mnNewMenu_2.add(mntmNewMenuItem_7);
		
		JMenuItem mntmNewMenuItem_6 = new JMenuItem("另存为...");
		mntmNewMenuItem_6.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
				JFileChooser jfileChooser = new JFileChooser();
				jfileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				jfileChooser.setFileFilter(new FileNameExtensionFilter("codemaker配置文件","cmk"));
				jfileChooser.setDialogTitle("另存为");
				jfileChooser.setSelectedFile(new File(dateFormat.format(new Date())+".cmk"));
				if(jfileChooser.showSaveDialog(frmApplication) == JFileChooser.APPROVE_OPTION) {
					ConfigUtil.getInstance(RecentlyFile.configFilePath).saveAsFile(jfileChooser.getSelectedFile().getAbsolutePath());
				}
				progressBar.setValue(100);
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_6);
		
		JSeparator separator_1 = new JSeparator();
		mnNewMenu_2.add(separator_1);
		
		JMenuItem mntmNewMenuItem_8 = new JMenuItem("退出");
		mntmNewMenuItem_8.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frmApplication.dispose();
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_8);
		
		JMenu mnNewMenu = new JMenu("生成代码");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem_11 = new JMenuItem("mvn install");
		mnNewMenu.add(mntmNewMenuItem_11);
		mntmNewMenuItem_11.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ProcessWindow pw = new ProcessWindow();
				MavenService mavenService = new MavenService();
				new Thread(()-> {
					pw.setLocationRelativeTo(frmApplication);
					pw.setVisible(true);
				}).start();
				new Thread(()-> {
					mavenService.mvninstall();
					pw.dispose();
				}).start();
			}
			
		});
		
		JMenuItem mntmNewMenuItem_12 = new JMenuItem("SQL->Java");
		mntmNewMenuItem_12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SQLMakerUI apiMakerUI = new SQLMakerUI();
				new Thread(()-> {
					apiMakerUI.setLocationRelativeTo(frmApplication);
					apiMakerUI.setModal(true);
					apiMakerUI.setVisible(true);
				}).start();
			}
		});
		mnNewMenu.add(mntmNewMenuItem_12);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("生成项目");
		mntmNewMenuItem.setIcon(new ImageIcon(this.getClass().getResource("/code/icon/project.png")));
		mnNewMenu.add(mntmNewMenuItem);
		mntmNewMenuItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Thread projectMakerThread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						try {
							if(!chckbxNewCheckBox.isSelected()) {
								int chooseIndex = JOptionPane.showConfirmDialog(frmApplication, "是否初始化数据库", "请选择", JOptionPane.YES_NO_OPTION);
								switch (chooseIndex) {
									case 0:
										chckbxNewCheckBox.setSelected(true);
										saveConfig();
										break;
									case 1:
										chckbxNewCheckBox.setSelected(false);
										saveConfig();
										break;
									default:
										return;
								}
							}
							progressBar.setValue(0);
							ProjectMaker maker = new ProjectMaker();
							ProjectInfo projectInfoNoTable = ConfigUtil.getInstance(RecentlyFile.configFilePath).getProjectInfoNoTable();
							if(ConfigUtil.getInstance(RecentlyFile.configFilePath).getCodeMakerInfo().isInitdb()) {
								MysqlLocalTool.createdb(projectInfoNoTable);
								Thread.sleep(3000);
								MysqlLocalTool.importSQL(projectInfoNoTable);
								System.out.println("休息5秒继续，写代码速度太快，后面的跟不上……");
								Thread.sleep(3000);
							}
							progressBar.setValue(30);
							ProjectInfo projectInfo = ConfigUtil.getInstance(RecentlyFile.configFilePath).getProjectInfo();
							ProjectMaker.pathUtil = PathUtil.getInstance(projectInfo);
							try {
								maker.parent(projectInfo);
							} catch (Exception e2) {
								e2.printStackTrace();
								JOptionPane.showMessageDialog(frmApplication, e2.getMessage(), "测试连接", JOptionPane.ERROR_MESSAGE);
								return;
							}
							
							File assembleDestFile = new File(maker.pathUtil.getModelPath("assemble").getFile(),"target/"+projectInfo.getArtifactId()+"-1.0-SNAPSHOT.jar");
							if(!assembleDestFile.exists()) {
								maker.build("mvn install",projectInfo.getProjectPath()+"/"+projectInfo.getArtifactId());
							}
							maker.build("mvn mybatis-generator:generate",projectInfo.getProjectPath()+"/"+projectInfo.getArtifactId()+"/"+projectInfo.getArtifactId()+"-dal/");
							progressBar.setValue(60);
							ProjectMaker.makeAssemble(projectInfo);
							ProjectMaker.makeDal(projectInfo);
							progressBar.setValue(70);
							ProjectMaker.makeSecurity(projectInfo);
							progressBar.setValue(80);
							ProjectMaker.makeService(projectInfo);
							progressBar.setValue(90);
							ProjectMaker.makeConfig(projectInfo);
							ProjectMaker.makeController(projectInfo);
							ProjectMaker.afterBuild(projectInfo);
							progressBar.setValue(100);
							maker.killProcess();
						} catch (Exception e2) {
							JOptionPane.showMessageDialog(frmApplication, e2.getMessage(), "失败", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
				projectMakerThread.start();
			}
		});
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("生成Mybatis");
		mntmNewMenuItem_1.setIcon(new ImageIcon(this.getClass().getResource("/code/icon/code.png")));
		mnNewMenu.add(mntmNewMenuItem_1);
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				MybatisGenWindow mybatisGenWindow = new MybatisGenWindow();
				mybatisGenWindow.setLocationRelativeTo(frmApplication);
				mybatisGenWindow.setVisible(true);
				
//				Thread mybatisGeneratorThread = new Thread(new Runnable() {
//					
//					@Override
//					public void run() {
//						progressBar.setValue(0);
//						MybatisGenerator mybatisGenerator = new MybatisGenerator();
//						progressBar.setValue(50);
//						mybatisGenerator.make();
//						progressBar.setValue(100);
//					}
//				});
//				mybatisGeneratorThread.start();
			}
		});
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("生成服务");
		mnNewMenu.add(mntmNewMenuItem_2);
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					@Override
					public void run() {
						progressBar.setValue(50);
						ServiceMaker javaMaker = new ServiceMaker();
						ServiceInfo serviceInfo = ConfigUtil.getInstance(RecentlyFile.configFilePath).getServiceInfo();
						javaMaker.makeService(serviceInfo);
						progressBar.setValue(100);
					}
				}).start();
				
			}
		});
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("生成视图层");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						progressBar.setValue(50);
						try {
							ControllerMaker.make(ConfigUtil.getInstance(RecentlyFile.configFilePath).getControllerInfo());
						} catch (Exception e) {
							e.printStackTrace();
						}
						progressBar.setValue(100);
					}
				}).start();
			}
		});
		mnNewMenu.add(mntmNewMenuItem_3);
		
		JMenu mnNewMenu_1 = new JMenu("工具");
		menuBar.add(mnNewMenu_1);
		
		JMenuItem mntmNewMenuItem_4 = new JMenuItem("密钥生成");
		mntmNewMenuItem_4.setIcon(new ImageIcon(this.getClass().getResource("/code/icon/key.png")));
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				KeyPairGenerator keyPairGenerator = new KeyPairGenerator();
				keyPairGenerator.setLocationRelativeTo(frmApplication);
				keyPairGenerator.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_4);
		
		JMenuItem mntmNewMenuItem_9 = new JMenuItem("驼峰工具");
		mntmNewMenuItem_9.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				NameCoverWindow nameCoverWindow = new NameCoverWindow();
				nameCoverWindow.setLocationRelativeTo(frmApplication);
				nameCoverWindow.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_9);
		
		JMenuItem mntmNewMenuItem_10 = new JMenuItem("JSON生成类工具");
		mntmNewMenuItem_10.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JacksonParserUI jacksonParserUI = new JacksonParserUI();
				jacksonParserUI.setLocationRelativeTo(frmApplication);
				jacksonParserUI.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_10);
		
		JPanel controllerPanel = new JPanel();
		controllerPanel.setPreferredSize(new Dimension(10, 11));
		controllerPanel.setBorder(new TitledBorder(null, "\u89C6\u56FE\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		formPanel.add(controllerPanel, "2, 8, fill, fill");
		controllerPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel_12 = new JLabel("URL前缀");
		controllerPanel.add(lblNewLabel_12, "2, 2, right, default");
		
		textField_12 = new JTextField();
		controllerPanel.add(textField_12, "4, 2, fill, default");
		textField_12.setColumns(10);
		
		JLabel lblNewLabel_13 = new JLabel("模块名称");
		controllerPanel.add(lblNewLabel_13, "10, 2, right, default");
		
		textField_11 = new JTextField();
		controllerPanel.add(textField_11, "12, 2, fill, default");
		textField_11.setColumns(10);
		
		JLabel lblNewLabel_14 = new JLabel("父菜单ID");
		controllerPanel.add(lblNewLabel_14, "2, 4, right, default");
		
		JButton btnNewButton_4 = new JButton("选择...");
		btnNewButton_4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				MenuSelect dialog = new MenuSelect(textField_8.getText(), textField_7.getText(), textField_9.getText(),
						new String(passwordField.getPassword()));
				
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setModal(true);
				dialog.setLocationRelativeTo(frmApplication);
				dialog.setVisible(true);
				if(dialog.getMenuId() != null) {
					textField_4.setText(String.valueOf(dialog.getMenuId()));
				}
			}
		});
		
		textField_4 = new JTextField();
		textField_4.setEditable(false);
		controllerPanel.add(textField_4, "4, 4, fill, default");
		textField_4.setColumns(10);
		controllerPanel.add(btnNewButton_4, "6, 4");
		
		JLabel lblNewLabel_15 = new JLabel("父资源ID");
		controllerPanel.add(lblNewLabel_15, "10, 4, right, default");
		
		textField_5 = new JTextField();
		textField_5.setEditable(false);
		controllerPanel.add(textField_5, "12, 4, fill, default");
		textField_5.setColumns(10);
		
		JButton btnNewButton_5 = new JButton("选择...");
		btnNewButton_5.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ResourceSelect resourceSelect = new ResourceSelect(textField_8.getText(), textField_7.getText(), textField_9.getText(),
						new String(passwordField.getPassword()));
				resourceSelect.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				resourceSelect.setModal(true);
				resourceSelect.setLocationRelativeTo(frmApplication);
				resourceSelect.setVisible(true);
				if(resourceSelect.getResourceId() != null) {
					textField_5.setText(String.valueOf(resourceSelect.getResourceId()));
				}
			}
		});
		controllerPanel.add(btnNewButton_5, "14, 4");
		initializeForm();
	}
	
	public void initializeForm() {
		// 最近文件
		RecentlyFile.loadMenus(recentlyMenu,this);
		try {
			if(ConfigUtil.getInstance(RecentlyFile.configFilePath).getConfigPath()==null)
				return;
			config.load(new InputStreamReader(new FileInputStream(ConfigUtil.getInstance(RecentlyFile.configFilePath).getConfigPath()), "utf-8"));
			frmApplication.setTitle("CodeMaker - "+ConfigUtil.getInstance(RecentlyFile.configFilePath).getConfigPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
		textField.setText(config.getProperty("projectinfo.projectPath"));
		textField_1.setText(config.getProperty("projectinfo.artifactId"));
		textField_7.setText(config.getProperty("projectinfo.dbname"));
		textField_8.setText(config.getProperty("projectinfo.dbhost"));
		passwordField.setText(config.getProperty("projectinfo.dbpasswd"));
		textField_9.setText(config.getProperty("projectinfo.dbusername"));
		textField_2.setText(config.getProperty("projectinfo.companyName"));
		textField_3.setText(new String(config.getProperty("projectinfo.description")));
		//controller
		textField_12.setText(new String(config.getProperty("controller.url.prefix")));
		textField_11.setText(new String(config.getProperty("controller.modelname")));
		textField_4.setText(new String(config.getProperty("controller.parentMenuId")));
		textField_5.setText(new String(config.getProperty("controller.parentResourceId")));
		// service
		textField_10.setText(new String(config.getProperty("serviceInfo.serviceName")));
		textField_6.setText(new String(config.getProperty("serviceInfo.modelName")));
		
		spinner.setValue(Integer.parseInt(config.getProperty("projectinfo.serverport")));
		String initdb = config.getProperty("codemaker.initdb");
		if(StringUtils.isNotBlank(initdb)) {
			if(initdb.equalsIgnoreCase("true")) {
				chckbxNewCheckBox.setSelected(true);
			} else {
				chckbxNewCheckBox.setSelected(false);
			}
		} else {
			chckbxNewCheckBox.setSelected(false);
		}
	}
	
	public void newConfig() {
		textField.setText("");
		textField_1.setText("");
		textField_7.setText("");
		textField_8.setText("");
		passwordField.setText("");
		textField_9.setText("");
		textField_2.setText("");
		textField_3.setText("");
		spinner.setValue("0");
		chckbxNewCheckBox.setSelected(false);
		textField_6.setText("");
		textField_10.setText("");
		textField_12.setText("");
		textField_11.setText("");
		textField_4.setText("");
		textField_5.setText("");
	}

	public void saveConfig() {
		config.setProperty("projectinfo.projectPath", textField.getText());
		config.setProperty("projectinfo.artifactId", textField_1.getText());
		config.setProperty("projectinfo.dbname", textField_7.getText());
		config.setProperty("projectinfo.dbhost", textField_8.getText());
		config.setProperty("projectinfo.dbpasswd", new String(passwordField.getPassword()));
		config.setProperty("projectinfo.dbusername", textField_9.getText());
		config.setProperty("projectinfo.companyName", textField_2.getText());
		config.setProperty("projectinfo.description", textField_3.getText());
		config.setProperty("projectinfo.serverport", String.valueOf(spinner.getValue()));
		config.setProperty("codemaker.initdb", Boolean.toString(chckbxNewCheckBox.isSelected()));
		config.setProperty("serviceInfo.modelName", textField_6.getText());
		config.setProperty("serviceInfo.serviceName", textField_10.getText());
		// view
		config.setProperty("controller.url.prefix", textField_12.getText());
		config.setProperty("controller.modelname", textField_11.getText());
		config.setProperty("controller.parentMenuId", textField_4.getText());
		config.setProperty("controller.parentResourceId", textField_5.getText());
		try {
			config.store(new FileWriter(ConfigUtil.getInstance(RecentlyFile.configFilePath).getConfigPath()), "updated by ui");
		} catch (Exception e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(frmApplication, e1.getMessage(), "保存配置", JOptionPane.ERROR_MESSAGE);
			return;
		}
	}
}
